package com.medicine.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.medicine.service.impl.AdminService;

import po.Admin;
import po.T_gly;
import po.Teacher;
import po.Zgxx;
import requestvo.UserReqVo;

@Controller
public class UserController extends BaseController {

	@RequestMapping("/login")
	public void login(UserReqVo userReqVo, HttpServletRequest request,
			HttpServletResponse response, HttpSession session)
			throws IOException {
		Logger logger = Logger.getLogger(getClass().getName());
		Admin t_admin = new Admin();
		Teacher teacher = new Teacher();
		String type = request.getParameter("userType");
		t_admin.setGly_no(userReqVo.getUserId());
		t_admin.setGly_psw(userReqVo.getPassword());
		teacher.setTech_no(userReqVo.getUserId());
		teacher.setTech_psw(userReqVo.getPassword());
		// 1实验室管理员
		if (type.equals("1")) {
			try {
				String username = adminService.hasAdmin(userReqVo.getUserId(),userReqVo.getPassword());
				if (username != null && !username.equals("")) {
					session.setAttribute("user", username);
					session.setAttribute("userType", "实验室管理员");
					response.sendRedirect("medicineAdmin.jsp");
				} else {
					response.setContentType("text/html;charset=UTF-8");
					PrintWriter out = response.getWriter();
					int status=-1;
					out.print(status);// 返回登录信息
					out.flush();
					out.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
				response.sendRedirect("500.jsp");
			}
			// 2为教师登录
		} else if (type.equals("2")) {
			try {
				String username = teacherService.hasTeacher(userReqVo.getUserId(), userReqVo.getPassword());
				if (username != null && !username.equals("")) {
					session.setAttribute("user", username);
					session.setAttribute("userType", "教师");
					response.sendRedirect("medicineTeacher.jsp");

				} else {
					response.setContentType("text/html;charset=UTF-8");
					PrintWriter out = response.getWriter();
					int status=-1;
					out.print(status);// 返回登录信息
					out.flush();
					out.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
				response.sendRedirect("500.jsp");
			}
		}else if(type.equals("3")){ //都不是则代表超级管理员登录
			try {
				String username = superAdminService.hasSuperAdmin(userReqVo.getUserId(), userReqVo.getPassword());
				if (username != null && !username.equals("")) {
					session.setAttribute("user", username);
					session.setAttribute("userType", "系统管理员");
					response.sendRedirect("medicineSuperAdmin.jsp");
				} else {
					response.setContentType("text/html;charset=UTF-8");
					PrintWriter out = response.getWriter();
					int status=-1;
					out.print(status);// 返回登录信息
					out.flush();
					out.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
				response.sendRedirect("500.jsp");
			}
		}

	}
	@RequestMapping("/outLogin")
	public void outlogin(HttpServletResponse response,HttpSession session ) throws IOException{
			session.removeAttribute("user");
			response.sendRedirect("login.html");	
	}
}
